/**
 * @file GetNewSubscriptionHandler.cs
 * @author Zane Mayo
 * @date 20/5/2009
 */

using System;
using System.Collections.Generic;

namespace MxitAPI {
	
	
	public class GetNewSubscriptionHandler : Handler {
		
        public event NewSubscriptionEventHandler NewSubscription;

		private static log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
		
		public GetNewSubscriptionHandler(): base( typeof( GetNewSubscriptionProtocolMessage ) ) {
			log.Debug( "New GetNewSubscriptionHandler created" );
		}
		
		protected override void HandleSpecificResponseProtocolMessage( ProtocolMessage message ) {
			GetNewSubscriptionProtocolMessage getNewSubscriptionProtocolMessage =  message as GetNewSubscriptionProtocolMessage;
			if ( getNewSubscriptionProtocolMessage == null ) 
				throw new MxitException( "Could not handle response. The message is of the wrong type." );
			
			List<RawSubscription> rawSubscriptions = getNewSubscriptionProtocolMessage.RawSubscriptions;
			
			// Add each raw Mxit contact to the list of all contacts and get its unque Id
			foreach ( RawSubscription rawSubscription in rawSubscriptions ) {
                Contacts.Instance.AddSubscription( rawSubscription );
				log.Debug( "New subscription request: " + rawSubscription.ToString() );
				//int contactId = Contacts.Instance.AddContact( rawContact );
				//Console.WriteLine( contactId );
			}
			
		}
		
		
	}
}
